package com.mls.fivedistrict.controller.backend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mls.fivedistrict.common.PageFilter;
import com.mls.fivedistrict.common.RequestHolder;
import com.mls.fivedistrict.common.ServiceResponse;
import com.mls.fivedistrict.controller.BaseController;
import com.mls.fivedistrict.exception.CustomerException;
import com.mls.fivedistrict.pojo.GoodsAdvertising;
import com.mls.fivedistrict.server.GoodsAdvertisingService;
import com.mls.fivedistrict.util.validator.BeanValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Optional;

/**
 * @author chenhua
 * @date  2018/5/17 13:39
 */

@RestController("backendGoodsAdvertisingController")
@RequestMapping("/api/manager")
public class GoodsAdvertisingController extends BaseController {
    @Autowired
    GoodsAdvertisingService goodsAdvertisingService;

    /**
     * 新增商品
     * @param goodsAdvertising
     * @return
     */
    @PostMapping("/v1/goodsAdvertising")
    public ServiceResponse addGoodsAdvertising(@RequestBody GoodsAdvertising goodsAdvertising){
        BeanValidator.check(goodsAdvertising);
        boolean flag=goodsAdvertisingService.insert(goodsAdvertising);
        if(flag){
            return ServiceResponse.createSuccessResponse("新增商品广告成功");
        }else{
            return ServiceResponse.createErrorResponse("新增商品广告失败");
        }
    }

    /**
     * 根据id修改
     * @param goodsAdvertisingId
     * @param goodsAdvertising
     * @return
     */
    @PutMapping("/v1/goodsAdvertising/{goodsAdvertisingId}")
    public ServiceResponse editGoodsAdvertising(@PathVariable Long goodsAdvertisingId,@RequestBody  GoodsAdvertising goodsAdvertising){
        Optional.ofNullable(goodsAdvertisingId).orElseThrow(()->new CustomerException("要修改的商品广告id不能为空"));
        Optional.ofNullable(goodsAdvertising).orElseThrow(()->new CustomerException("要修改的商品广告不能为空"));
        goodsAdvertising.setId(goodsAdvertisingId);
        boolean flag=goodsAdvertisingService.updateById(goodsAdvertising);
        if(flag){
            return ServiceResponse.createSuccessResponse("修改商品广告成功");
        }else{
            return ServiceResponse.createErrorResponse("修改商品广告失败");
        }
    }

    /**
     * 根据id删除
     * @param goodsAdvertisingId
     * @return
     */
    @DeleteMapping("/v1/goodsAdvertising/{goodsAdvertisingId}")
    public ServiceResponse deleteGoodsAdvertising(@PathVariable Long goodsAdvertisingId){
        Optional.ofNullable(goodsAdvertisingId).orElseThrow(()->new CustomerException("要删除的商品广告id不能为空"));
        GoodsAdvertising goodsAdvertising = new GoodsAdvertising();
        goodsAdvertising.setIsDelete(true);
        goodsAdvertising.setId(goodsAdvertisingId);
        boolean flag = goodsAdvertisingService.updateById(goodsAdvertising);
        if(flag){
            return ServiceResponse.createSuccessResponse("删除商品的该条广告成功");
        }else{
            return  ServiceResponse.createErrorResponse("删除商品的该条广告失败");
        }
    }

    /**
     * 分页查询广告列表
     * @return
     */
    @GetMapping("/v1/goodsAdvertisings")
    public ServiceResponse getGoodsAdvertisingList(){
        PageFilter pageFilter = RequestHolder.getPageFilter();
        PageHelper.startPage(pageFilter.getPageNum(), pageFilter.getPageSize());
        List selectList = goodsAdvertisingService.selectList(pageFilter.getWrapper());
        PageInfo<GoodsAdvertising> goodsAdvertisingPageInfo = new PageInfo<GoodsAdvertising>(selectList, 5);
        return  ServiceResponse.createSuccessResponse(goodsAdvertisingPageInfo);
    }

    /**
     * 根据id查询商品广告
     * @param goodsAdvertisingsId
     * @return
     */
    @GetMapping("/v1/goodsAdvertising/{goodsAdvertisingsId}")
    public ServiceResponse getGoodsAdvertising(@PathVariable Long goodsAdvertisingsId){
        Optional.ofNullable(goodsAdvertisingsId).orElseThrow(()->new CustomerException("要查询的商品广告id不能为空"));
        GoodsAdvertising goodsAdvertising = goodsAdvertisingService.selectById(goodsAdvertisingsId);
        if(null!=goodsAdvertising){
            return ServiceResponse.createSuccessResponse(goodsAdvertising);
        }else{
            return ServiceResponse.createErrorResponse("商品广告不存在");
        }
    }

}
